import java.awt.Color;
import java.awt.Graphics;


public class Character_Skeleton {
	
	final static int TOLERANCE = 20;
	final static double RADIO = 2;
	final static int HEAD_SIZE = 15;
	final static int NECK_SIZE = 10;
	final static int TORSO_SIZE = 100;
	final static int ARM_SIZE = 50;
	final static int FOREARM_SIZE = 50;
	final static int THIGH_SIZE = 60;
	final static int CALF_SIZE = 60;
	
	final static int HEAD_WIDTH = 40;
	final static int HEAD_HEIGHT = 45;
	final static int TORSO_WIDTH = 105;
	final static int TORSO_HEIGHT =161;
	final static int BICEP_WIDTH = 84;
	final static int BICEP_HEIGHT = 28;
	final static int FOREARM_WIDTH = 54;
	final static int FOREARM_HEIGHT =24;
	final static int THIGH_WIDTH = 41;
	final static int THIGH_HEIGHT = 96;
	final static int LEG_WIDTH = 31;
	final static int LEG_HEIGHT =105;
	
	BodyPart Head, Neck, Torso, RightArm, LeftArm, RightForeArm, LeftForeArm, RightThigh, LeftThigh, RightCalf, LeftCalf;
	
	BodyPart gndThigh, actThigh, gndCalf, actCalf;
	
	Boolean isRunning;
	
	double angulerVelocity;
	
	public Character_Skeleton(ydPoint startPostion){
		
		Neck=new BodyPart(startPostion, NECK_SIZE);
		Torso =new BodyPart(Neck.limb.p2, TORSO_SIZE);
		
		RightArm=new BodyPart(Neck.limb.p2, ARM_SIZE);
		LeftArm=new BodyPart(Neck.limb.p2, ARM_SIZE);
		RightForeArm=new BodyPart(RightArm.limb.p2, FOREARM_SIZE);
		LeftForeArm=new BodyPart(LeftArm.limb.p2, FOREARM_SIZE);
		
		RightThigh=new BodyPart(Torso.limb.p2, THIGH_SIZE);
		LeftThigh=new BodyPart(Torso.limb.p2, THIGH_SIZE);
		RightCalf=new BodyPart(RightThigh.limb.p2, CALF_SIZE);
		LeftCalf=new BodyPart(LeftThigh.limb.p2, CALF_SIZE);
		
		
		angulerVelocity=0.05;
		
		isRunning=false;
		
	}
	
	public void draw(Graphics g){
		Neck.draw(g);
		g.setColor(Color.BLUE);
		Torso.draw(g);
		RightArm.draw(g);
		LeftArm.draw(g);
		RightForeArm.draw(g);
		LeftForeArm.draw(g);
		g.setColor(Color.GREEN);
		RightThigh.draw(g);
		RightCalf.draw(g);
		g.setColor(Color.YELLOW);
		
		LeftThigh.draw(g);
		LeftCalf.draw(g);	
	}
	
	public void run(){
		
		if(!isRunning){
		//if(LeftCalf.limb.p2.x>RightCalf.limb.p2.x){
			gndThigh=LeftThigh;
			gndCalf=LeftCalf;
			
			actThigh=RightThigh;
			actCalf=RightCalf;
			
			angulerVelocity=0.5;
	//	}
	//	else{
		/*	gndThigh=RightThigh;
			gndCalf=RightCalf;
			
			actThigh=LeftThigh;
			actCalf=LeftCalf;*/
	//	}
		
		
			setRunSpeeds(0);
		isRunning=true;
		}
		
		
		
		/*if(angulerVelocity<0.5)
		angulerVelocity+=0.01;*/
		
		//runActiveLeg();
		runActiveLeg(actThigh,actCalf);
		runGndLeg(gndThigh,gndCalf);
		//runActiveLeg(gndThigh,gndCalf);
			
	}
	
	public void stopRunning(){
		gndThigh.angularVelocity=0;
		gndCalf.angularVelocity=0;
		
		actThigh.angularVelocity=0;
		actCalf.angularVelocity=0;
	}
	
	public void setRunSpeeds(int i){
		
		if(i==0){
		gndThigh.angularVelocity=-ydMath.degreesToRads((float) angulerVelocity);
		gndCalf.angularVelocity=ydMath.degreesToRads((float) (angulerVelocity*2));
		
		actThigh.angularVelocity=-ydMath.degreesToRads((float) angulerVelocity*2);
		actCalf.angularVelocity=ydMath.degreesToRads((float) (angulerVelocity*7));
		}
		else{
			gndThigh.angularVelocity=-ydMath.degreesToRads((float) angulerVelocity*2);
			gndCalf.angularVelocity=ydMath.degreesToRads((float) (angulerVelocity*4));
			
			actThigh.angularVelocity=-ydMath.degreesToRads((float) angulerVelocity*2);
			actCalf.angularVelocity=ydMath.degreesToRads((float) (angulerVelocity*7));
		}
	}
	
/*	public void runActiveLeg(){
		ydPoint pivot=actThigh.limb.p1;
		
		
		if(actThigh.limb.angle>180){
			actThigh.angularVelocity*=-1;
			//actThigh.limb.rotate(pivot, ydMath.degreesToRads((float) (180-actThigh.limb.angle)));
		}
		else if(actThigh.limb.angle<0){
			actThigh.angularVelocity*=-1;
			//actThigh.limb.rotate(pivot, ydMath.degreesToRads((float) (-actThigh.limb.angle)));
		}
		
		if(actCalf.limb.angle>180){
			actCalf.angularVelocity*=-1;
			//actCalf.limb.rotate(pivot, ydMath.degreesToRads((float) (180-actCalf.limb.angle)));
		}
		else if(actCalf.limb.angle<0){
			actCalf.angularVelocity*=-1;
			//actCalf.limb.rotate(pivot, ydMath.degreesToRads((float) (-actCalf.limb.angle)));
		}
		
		if(actCalf.limb.angle>180 || actCalf.limb.angle<0)
			actCalf.angularVelocity*=-1;
		
		actThigh.limb.rotate(pivot,actThigh.angularVelocity);
		
		actCalf.limb.rotate(pivot, actCalf.angularVelocity);
		actCalf.limb.move(actThigh.limb.p2);
	}*/
	
	public void runActiveLeg(BodyPart b1, BodyPart b2){
		ydPoint pivot=b1.limb.p1;
		
		
		/*if(b1.limb.angle>135 || b1.limb.angle<45)
			b1.angularVelocity*=-1;*/
		
		if(b2.limb.angle>135 || b2.limb.angle<45 ||  b1.limb.angle>b2.limb.angle){
			//b2.limb.rotate(pivot, ydMath.degreesToRads((float) (180-b2.limb.angle)));
			b2.angularVelocity*=-1;
		}
		
		b1.limb.rotate(pivot,b1.angularVelocity);
		
		b2.limb.rotate(pivot, b2.angularVelocity);
		b2.limb.move(b1.limb.p2);
	}
	
	public void runGndLeg(BodyPart b1, BodyPart b2){
		ydPoint pivot=b2.limb.p2;
		
		
/*		if(b1.limb.angle>135 || b1.limb.angle<45)
			b1.angularVelocity*=-1;
		
		if(b2.limb.angle>135 || b2.limb.angle<45 ||  b1.limb.angle>b2.limb.angle)
			b2.angularVelocity*=-1;*/
		
		b1.limb.rotate(b1.limb.p2,-b1.angularVelocity);
		
		b2.limb.rotate(pivot, b2.angularVelocity);
		//b2.limb.move(b1.limb.p2);
		
		//b2.limb.move(new ydPoint(300, 530),0);
		
		b1.limb.move(b2.limb.p1, 0);
		Torso.limb.move(b1.limb.p1, 0);
		actThigh.limb.move(b1.limb.p1);
		actCalf.limb.move(actThigh.limb.p2);
		
		if(b2.limb.angle>135){
			/*actThigh.limb.rotate(pivot, ydMath.degreesToRads((float) -(45-actThigh.limb.angle)));
			actCalf.limb.rotate(pivot, ydMath.degreesToRads((float) -(45-actCalf.limb.angle)));
			
			gndThigh.limb.rotate(pivot, ydMath.degreesToRads((float) -(112.5-gndThigh.limb.angle)));
			gndCalf.limb.rotate(pivot, ydMath.degreesToRads((float) -(135-gndCalf.limb.angle)));*/
			
			BodyPart tmp1, tmp2;
			
			tmp1=actCalf;
			actCalf=gndCalf;
			gndCalf=tmp1;
			
			tmp1=actThigh;
			actThigh=gndThigh;
			gndThigh=tmp1;
			
			/*BodyPart.swap(actCalf, gndCalf);
			BodyPart.swap(actThigh, gndThigh);*/
			
			setRunSpeeds(1);

			
			return;
		}
	}

}
